package com.quanyan.club.thread;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.RespClubBaseInfo;
import com.quanyan.club.entity.db.TbClub;
import com.quanyan.club.util.ClubNameUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.common.vo.resp.RespCity;
import com.quanyan.common.vo.resp.RespDistrict;
import com.quanyan.search.entity.vo.ClubIndexInfo;
import com.quanyan.search.service.impl.ClubSearchServiceImpl;

import org.apache.commons.lang.time.StopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhuo.shi on 2016/10/20.
 */

public class BatchClubSolrIndexThread implements  Runnable{

    private Logger logger = LoggerFactory.getLogger(ClubSolrIndexThread.class);

    private boolean isUpdate;     //1 插入 2 更新

    private List<RespClubBaseInfo> clubBaseInfos;
    private List<Integer> clubIds;
    private ClubSearchServiceImpl clubSearchService;

    public BatchClubSolrIndexThread(List<RespClubBaseInfo> clubBaseInfo, List<Integer> clubIds,  ClubSearchServiceImpl clubSearchService, boolean isUpdate){
        this.clubBaseInfos = clubBaseInfo;
        this.clubSearchService = clubSearchService;
        this.clubIds = clubIds;
        this.isUpdate = isUpdate;
    }

    @Override
    public void run() {

        if (isUpdate){
            logger.warn("-----------------------开始删除--------------------------");
            //先删除
            long t2 = System.currentTimeMillis();
            //先删除
            APIResponse response = clubSearchService.deleteDocsByIds(clubIds);
            if (!response.isRet()){
                logger.warn("删除俱乐部"+ clubIds +"索引失败，"+ response.getErrmsg());
            }
            logger.warn("删除俱乐部all索引，花费：{}, {}", JSON.toJSON(response), System.currentTimeMillis()-t2);
        }

        List<ClubIndexInfo> indexInfoList = new ArrayList<>();
        for (RespClubBaseInfo clubBaseInfo : clubBaseInfos) {

            ClubIndexInfo indexInfo = new ClubIndexInfo();
            indexInfo.setClub_id(String.valueOf(clubBaseInfo.getClubId()));
            indexInfo.setClub_name(clubBaseInfo.getClubName());
            String clubNamePY = ClubNameUtils.getClubNamePinYin(clubBaseInfo.getClubName());
            indexInfo.setClub_name_pinyin(clubNamePY);
            if (!StringUtil.isBlankOrNull(clubNamePY)) {
                indexInfo.setClub_name_first_letter(clubNamePY.substring(0, 1));
            }
            RespCity city = clubBaseInfo.getCity();
            if (city != null) {
                indexInfo.setClub_city_id(city.getId());
                indexInfo.setClub_city_name(city.getName());
            }
            RespDistrict district = clubBaseInfo.getDistrict();
            if (district != null) {
                indexInfo.setClub_district_id(district.getId());
                indexInfo.setClub_district_name(district.getName());
            }
            indexInfo.setClub_logo(clubBaseInfo.getClubLogo());
            indexInfo.setClub_founder_nickname(clubBaseInfo.getUsername());
            indexInfo.setClub_founder_uid(clubBaseInfo.getUid());
            indexInfo.setClub_sport_name(clubBaseInfo.getSportName());
            indexInfo.setClub_star(clubBaseInfo.getStar() == null ? 0d : clubBaseInfo.getStar());
            indexInfo.setClub_integral(clubBaseInfo.getIntegral() == null ? 0 : clubBaseInfo.getIntegral());
            indexInfo.setClub_level(clubBaseInfo.getLevel() == null ? 0 : clubBaseInfo.getLevel());
            indexInfo.setClub_type_id(clubBaseInfo.getClubType());
            indexInfo.setResident_place_id(clubBaseInfo.getResidentPlaceId());
            indexInfo.setClub_lat(clubBaseInfo.getLat() == null? 0.0: clubBaseInfo.getLat());
            indexInfo.setClub_lng(clubBaseInfo.getLng() == null? 0.0 : clubBaseInfo.getLng());
            indexInfo.setClub_activity_count(clubBaseInfo.getGoingActivityCount());
            logger.warn("clubIndex=" + JSON.toJSONString(indexInfo));

            indexInfoList.add(indexInfo);
        }

        long t1 = System.currentTimeMillis();
        APIResponse response = clubSearchService.insertDocs(indexInfoList);
        if (response == null || !response.isRet()) {
            logger.warn("all 添加俱乐部索引失败");
            logger.warn("返回：" + JSON.toJSON(response));
        } else {
            logger.debug("添加俱乐部all索引成功");
        }

        logger.warn("添加俱乐部all索引，花费：{}", System.currentTimeMillis() - t1);
    }

}
